import ast
import pandas as pd
from script_convert_rating import filter_unused_data


def convert_ratings(input_fname, output_fname):
    rating = pd.DataFrame([], columns=['userID', 'itemID', 'rating'])
    with open(input_fname, 'r') as f:
        text = f.read()
        line_l = text.split("\n")
        for line in line_l:
            if line == '':
                continue
            jins = ast.literal_eval(line)
            rating = rating.append({'userID': jins['username'], 'itemID': jins['product_id'], 'rating': jins['hours']},
                                   ignore_index=True)

    rating = filter_unused_data.convert_ratings(rating)

    order_l = ['userID', 'itemID', 'rating']
    rating = rating[order_l]
    rating.to_csv(output_fname, index=False)
    print("complete {}".format(output_fname))
